package com.lin.controller;

import com.lin.annotation.MyLog;
import com.lin.pojo.User;
import com.lin.services.UserService;
import com.lin.services.WaresService;
import com.lin.utils.ShiroUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * ClassName:LoginController
 * Package:com.lin.controller
 * Author: hellcat
 * Date: 2019/12/16
 * ♣♣♣ 登陆管理
 */
@Controller
@RequestMapping("/user")
public class LoginController {
    @Autowired
    private WaresService waresService;
    @Autowired
    private UserService userService;

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> login(HttpServletRequest request, HttpSession session) {
        String accountNumber = request.getParameter("username");
        String password = request.getParameter("password");
//        在日志aspect中用到
        session.setAttribute("loginUserName", accountNumber);
        Map<String, Object> map = new HashMap<>();
        if (accountNumber != null && password != null && !"".equals(accountNumber.trim()) && !"".equals(password.trim())) {
            Subject subject = ShiroUtil.getSubject();
            UsernamePasswordToken token = new UsernamePasswordToken(accountNumber, password);
            try {
                subject.login(token);
//                session.setAttribute("USER_SESSION", subject.getPrincipal());
                map.put("code", true);
            } catch (UnknownAccountException | IncorrectCredentialsException ue) {
                map.clear();
                map.put("code", false);
                map.put("msg", "账号或密码不正确");
            } catch (LockedAccountException le) {
                map.clear();
                map.put("code", false);
                map.put("msg", le.getMessage());
            }
        } else {
            map.put("code", false);
            map.put("msg", "异常输入。");
        }
        return map;
    }

    @RequestMapping(value = "/register", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> register(User user, HttpServletRequest request) {
        return userService.registerCheck(user);
    }

    @RequestMapping(value = "/Index", method = RequestMethod.GET)
    public String toIndex(HttpServletRequest request) {
        Integer storeId = ShiroUtil.getUser().getStoreId();
        if (storeId == null) {
            request.setAttribute("ddId", "Store");
            return "storeMain";
        }
        request.setAttribute("ddId", "Index");
//        过期提醒 库存不足提醒
        Map<String, Object> map = waresService.checkMessage();
        request.setAttribute("checkMessage", map);
        return "index";
    }


    @RequestMapping(value = "/toRegister", method = RequestMethod.GET)
    public String toRegister(HttpServletRequest request) {
        return "register";
    }

    @RequestMapping(value = "/toLogin", method = RequestMethod.GET)
    public String toLogin() {
        return "redirect:/";
    }

    @RequestMapping("/logout")
    public String logout() {
        try {
            ShiroUtil.clearAllCachedInfo(ShiroUtil.getUser().getAccountNumber());
//            ShiroUtil.clearAllCachedInfo();
            SecurityUtils.getSubject().logout();
        } catch (Exception e) {
            System.out.println("缓存清理失败");
        }
        return "redirect:/";
    }
}
